$(document).ready(function () {
    getData(p)
});

function getSortType(){
    var blood = ''
    if($('#searchSelect').css('display') == 'block'){
        switch ($("#searchSelect option:selected").val()) {
            case 'beforBreak':
                blood = 1
                break;
            case 'afterBreak':
                blood = 2
                break;
            case 'beforLunch':
                blood = 3
                break;
            case 'afterLunch':
                blood = 4
                break;
            case 'beforDinner':
                blood = 5
                break;
            case 'afterDinner':
                blood = 6
                break;
        }
    }
    return blood;
}

function getData(p){
    var startTime = $('#start').val() != ''? $('#start').val():'2019-01-01';//开始时间
    var endTime = $('#end').val() != ''? $('#end').val(): laydate.now(1, 'YYYY-MM-DD');//结束时间
    $.ajax({
        url: global.apiUrl + '/admin_get_user_xuetang',
        type: 'GET',
        dataType: 'json',
        data: {
            p: p,
            start_time: startTime,
            end_time: endTime,
            type: 1,
            blood: getSortType(),
            userid: $('#searchInput').val()
        },
        header: {
            'content-type': 'application/json' //默认值
        },
        success: function(res){
            // console.log(res)
            var html = ''
            for(var i in res.content){
                html += `<tr>
                            <td>
                                <div class="userItem" onclick="findId(${res.content[i].user_id})">
                                    <text>${res.content[i].user_content.nickname}</text>
                                    <text>${res.content[i].user_content.sex} ${res.content[i].user_content.height}cm</text>
                                    <text style="margin-bottom:0">ID: ${res.content[i].user_id}</text>
                                </div>
                            </td>
                            <td>${res.content[i].blood?res.content[i].blood:'-'}</td>
                            <td>${getSugarType(res.content[i].type)}</td>
                            <td>${res.content[i].date}</td>
                            <td>${res.content[i].addtime.substring(11,19)}</td>
                            <td>${res.content[i].eid?res.content[i].eid:'-'}</td>
                            <td>${res.content[i].content?res.content[i].content:'-'}</td>
                            <td>
                                <a href="sugarChart.html?id=${res.content[i].user_id}" class="btn btn-white btn-sm">趋势</a>
                            </td>
                        </tr>`
            }
            $('#userList').html(html)
            $("#page").paging({
                pageNo: p,
                totalPage: Math.ceil(res.count/20),
                totalSize: res.count,
                callback: function(num) {
                    // console.log(num)
                    getData(num,getValue(page))
                }
            })
        }
    })
}

function getSugarType(type){
    var text  = ''
    switch (type) {
        case '1':
            text = '早餐前'
            break;
        case '2':
            text = '早餐后'
            break;
        case '3':
            text = '午餐前'
            break;
        case '4':
            text = '午餐后'
            break;
        case '5':
            text = '晚餐前'
            break;
        case '6':
            text = '晚餐后'
            break;
        default:
            text = '未确认'
            break;
    }
    return text
}

//搜索
function search(obj)
{
    var searchInput = document.getElementById("searchInput");
    var searchSelect = document.getElementById("searchSelect");
    if(obj.value == 'ID'){
        searchInput.style.display = 'block';
        searchSelect.style.display = 'none'
    }else{
        searchInput.style.display = 'none';
        searchSelect.style.display = 'block'
        getData(p)
    }
}

function clickBtn(){
    getData(p)
}

function findId(id){
    // console.log(id)
    $("#searchSelectMain").val("ID"); 
    $("#searchInput").val(id);
    getData(p)
}